package com.b2c.ftysys.sevice.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.create.NewCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.b2c.ftysys.dao.B2cFsyStaffDAO;
import com.b2c.ftysys.dao.DatabaseException;
import com.b2c.ftysys.domain.B2cFsyStaff;
import com.b2c.ftysys.domain.B2cFsyStaffExample;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.Page;
import com.b2c.ftysys.domain.B2cFsyStaffExample.Criteria;
import com.b2c.ftysys.helper.ObjectUtils;
import com.b2c.ftysys.sevice.StaffInfoService;
import com.b2c.ftysys.view.application.AppErrorException;
import com.b2c.ftysys.view.application.fileImport.FileImport;

@Service
@RemoteProxy(creator = NewCreator.class, name = "staffService")
public class StaffInfoServiceImpl implements StaffInfoService {

	@Autowired
	private B2cFsyStaffDAO staffDao;

	@Resource(name = "staffFileImportor")
	private FileImport convert;

	public B2cFsyStaffDAO getStaffDao() {
		return staffDao;
	}

	public void setStaffDao(B2cFsyStaffDAO staffDao) {
		this.staffDao = staffDao;
	}

	public int createStaff(B2cFsyStaff staff) {
		if (StringUtils.isEmpty(staff.getStaffName()))
			return 0;
		staffDao.save(staff);
		return 1;
	}

	public List<B2cFsyStaff> queryAllStaff(Page page) {
		B2cFsyStaffExample example = new B2cFsyStaffExample();
		page.setMessage_count(staffDao.countByExample(example));
		try {
			return staffDao.getListPage("product.abatorgenerated_selectAll",
					page.getStartOffset(), page.getPage_size());
		} catch (DatabaseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<B2cFsyStaff> queryStaffListByClause(B2cFsyStaff example) {
		B2cFsyStaffExample clause = new B2cFsyStaffExample();
		clause.setPageSize(100);
		Criteria clauseO = clause.createCriteria();

		if (example == null)
			example = new B2cFsyStaff();
		if (example.getStaffId() != null && example.getStaffId() != 0)
			clauseO.andStaffIdEqualTo(example.getStaffId());

		if (example.getStaffName() != null
				&& example.getStaffName().trim().length() > 0)
			clauseO.andStaffNameEqualTo(example.getStaffName());

		if (null != example.getEnterTime()
				&& example.getEnterTime().trim().length() > 0)
			clauseO.andEnterTimeEqualTo(example.getEnterTime());

		return staffDao.selectByExample(clause);
	}

	public String getStaffName(Integer Id) {
		B2cFsyStaff staff = getStaffDao().selectByPrimaryKey(Id);

		if (staff != null)
			return staff.getStaffName();
		else
			return "";
	}

	public String getStaffNo(String staffName) {
		B2cFsyStaffExample example = new B2cFsyStaffExample();
		example.createCriteria().andStaffNameEqualTo(staffName);

		String _staffName = "";
		List<B2cFsyStaff> staffs = getStaffDao().selectByExample(example);
		if (staffs != null && staffs.size() > 0) {
			if (staffs.size() == 1) {
				return staffs.get(0).getStaffId() + "";
			} else {
				boolean first = true;
				for (B2cFsyStaff staff : staffs) {
					if (first) {
						_staffName = staff.getStaffId() + "";
						first = false;
					} else {
						_staffName += "," + staff.getStaffId() + "";
					}
				}
			}
		}
		return _staffName;

	}

	public int removeStaff(int staffId) {
		return staffDao.deleteByPrimaryKey(staffId);
	}

	public int updateStaff(B2cFsyStaff staff) {
		return staffDao.updateByPrimaryKeySelective(staff);
	}

	public int ImportExcel(String fileAddr) throws AppErrorException {
		List<Map<String, Object>> data = convert.importData(fileAddr);

		B2cFsyStaff staff = null;
		int i = 0;
		for (Map<String, Object> dataMap : data) {
			try {
				staff = (B2cFsyStaff) ObjectUtils.mapToObj(dataMap,
						B2cFsyStaff.class);
				staffDao.save(staff);
				i++;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	/**
	 * 通过拼音字母索引查询员工姓名
	 */
	public List<B2cFsyStaff> queryByPinyin(String pinYinName) {
		B2cFsyStaffExample example = ExampleFactory.createStaffExample();
		example.createCriteria().andPinyinNameLike(pinYinName + "%");
		example.setPageSize(20);
		return this.staffDao.selectByExample(example);
	}

}
